/*
 * @(#)ScriptEngineGlobals.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.config.runtime;

import java.util.HashMap;
import java.util.Map;

import javax.script.ScriptEngine;

/**
 * @author Web Platform Development Team
 */
public class ScriptEngineGlobals {
    public static final String JAVASCRIPT_SCRIPT_ENGINE_NAME = "javascript";
    public static final String GROOVY_SCRIPT_ENGINE_NAME = "groovy";
    public static final String JRUBY_SCRIPT_ENGINE_NAME = "jruby";
    private static final String[] ENGINE_TYPES = new String[] {GROOVY_SCRIPT_ENGINE_NAME, JAVASCRIPT_SCRIPT_ENGINE_NAME, JRUBY_SCRIPT_ENGINE_NAME};
    public static final String DEFAULT_SCRIPT_ENGINE_TYPE = JAVASCRIPT_SCRIPT_ENGINE_NAME;
    public static final String DEFAULT_SCRIPT_PROPERTY_FILENAME = "script-config-file";
    public static final String DEFAULT_SCRIPT_LIBRARY_FILENAME = "scriptlib.jar";

    public static final javax.script.ScriptEngineManager SCRIPTENGINE_MANAGER = new javax.script.ScriptEngineManager();

    // script type engine 등록  key=type value=engine
    static final Map<String, ScriptEngine> SCRIPTENGINE_MAP = new HashMap<String, ScriptEngine>();
    static final Map<String, Map<String, ScriptEngine>> SCRIPTENGINE_MAP_WEBMODULE = new HashMap<String, Map<String, ScriptEngine>>();

    // script Bindings Map key=scriptid value=bindings
    // static final Map<String, Bindings> bindingsMap = new HashMap<String, Bindings>();
    // ScriptEngineInfo Map key=scriptid , value=ScriptEngineInfo

    public static final String SCRIPT_CALL_TYPE_PRE = "prescript";
    public static final String SCRIPT_CALL_TYPE_POST = "postscript";

    /**
     * Gets the script engine.
     *
     * @param engineType the engine type
     * @return the script engine
     */
    public static ScriptEngine getScriptEngine(String engineType) {
        return SCRIPTENGINE_MAP.get(engineType);
    }

    /**
     * Adds the script engine.
     *
     * @param engineType the engine type
     * @param engine     the engine
     */
    public static void addScriptEngine(String engineType, ScriptEngine engine) {
        SCRIPTENGINE_MAP.put(engineType, engine);
    }

    /**
     * Clear.
     */
    public static void clear() {
        SCRIPTENGINE_MAP_WEBMODULE.clear();
        SCRIPTENGINE_MAP.clear();
    }

    /**
     * Gets the script engine type.
     *
     * @param fileName the file name
     * @return the script engine type
     */
    public static String getScriptEngineType(String fileName) {
        String type = ScriptEngineGlobals.DEFAULT_SCRIPT_ENGINE_TYPE;

        if (fileName.endsWith(".groovy")) {
            type = ScriptEngineGlobals.GROOVY_SCRIPT_ENGINE_NAME;
        }
        /* else if (fileName.endsWith(".rb"))
              type = ScriptEngineGlobals.JRUBY_SCRIPT_ENGINE_NAME; 
         */
        return type;
    }

    /**
     * Gets the script engine for web module.
     *
     * @param contextName the context name
     * @return the script engine for web module
     */
    @SuppressWarnings("unchecked")
	public static Map getScriptEngineForWebModule(String contextName) {
        return SCRIPTENGINE_MAP_WEBMODULE.get(contextName);
    }

    /**
     * Adds the script engine for web module.
     *
     * @param modulePath the module path
     * @param engineMap  the engine map
     */
    public static void addScriptEngineForWebModule(String modulePath, Map<String, ScriptEngine> engineMap) {
        SCRIPTENGINE_MAP_WEBMODULE.put(modulePath, engineMap);
    }

    /**
     * Gets the engine type length.
     *
     * @return the engine type length
     */
    public static int getEngineTypeLength() {
        return ENGINE_TYPES.length;
    }

    /**
     * Gets the engine type.
     *
     * @param idx the idx
     * @return the engine type
     */
    public static String getEngineType(int idx) {
        return ENGINE_TYPES[idx];
    }

}
